<?php

namespace app\admin\controller;

use app\lib\enum\Status;
use think\Exception;
use think\Request;

class Website extends BaseController {
    //更新基本信息
    public function show() {
        if (!judgeRole(__METHOD__)) {
            $this->redirect("admin/index/forbidden");
        }
        if(request()->isAjax()){
            $data=Request::instance()->param();
            $data['web_status']=input("post.web_status")=="on"?Status::on:Status::off;
            $data['web_issendsms']=input("post.web_issendsms")=="on"?Status::on:Status::off;
            $result=model("CmsWebsite")->updateWebsiteInfo($data);
            $this->uniteResponse($result,"数据更新成功");
        }
        $webInfo=model("CmsWebsite")->select();
        $webInfo=$webInfo[0];
        $this->assign("webInfo",$webInfo);
        return view();
    }

    //数据库备份列表
    public function backup() {
//        $backup = new \org\Baksql(\think\Config::get("database"));
//        return json(array_reverse($backup->get_filelist()));


        if(request()->isAjax()){
            $backupInfo=model("CmsDatabaseBackup")
                ->order("bak_id","desc")
                ->select();
            foreach ($backupInfo as &$value){
                $adm_id=$value['bak_admin_id'];
                $adminInfo=model("CmsAdmin")->find($adm_id);
                $value['adm_realname']=$adminInfo['adm_realname'];
                $value['bak_create_time']=date("Y-m-d H:i:s",$value['bak_create_time']);
            }
            $count=count($backupInfo);
            return getJson($count,$backupInfo);
        }
        return view();
    }

    //添加备份
    public function backupsAdd(){
        if(request()->isAjax()){
            $bak_description=input("post.bak_description");
            $result=model("CmsDatabaseBackup")->addBackups($bak_description);
            if($result['status']==="success"){
                $this->success($result['msg']);
            }else{
                $this->error($result['msg']);
            }
        }
        return view();
    }

    //下载备份
    public function download(){
        if(session("adminInfo")){
            $fileName=input("get.bak_name");
            $backup = new \org\Baksql(\think\Config::get("database"));
            $backup->downloadFile($fileName);
        }
    }


    //删除备份
    public function delete(){
        $data=[
            "bak_id"=>input("get.bak_id"),
            "bak_name"=>input("get.bak_name")
        ];
        if(!checkBackupData($data)){
            $this->error("数据不合法");
        }

        $this->uniteResponse(model("CmsDatabaseBackup")->deleteBackups($data),"备份删除成功");
    }

    //备份数据还原
    public function restore(){
        $data=[
            "bak_id"=>input("get.bak_id"),
            "bak_name"=>input("get.bak_name")
        ];

        if(!checkBackupData($data)){
            $this->error("数据不合法");
        }
        $backup = new \org\Baksql(\think\Config::get("database"));
        $result=$backup->restore($data['bak_name']);
        if($result['status']==="success"){
            $this->success($result['msg']);
        }else{
            $this->error($result['msg']);
        }
    }

    //清理缓存
    public function cleanCache(){
       try{
           //删除缓存文件
//           delFileUnderDir(RUNTIME_PATH."cache");
           delFileUnderDir(RUNTIME_PATH."log");
           delFileUnderDir(RUNTIME_PATH."temp");

           //重新生成session与cache
           refreshWebInfoInCache();
           refreshAdminInfoInSession();
       }catch (Exception $exception){
           $this->error("缓存清除异常");
       }
       $this->success("缓存清除成功");
    }


    //处理图片上传
    public function uploadImg(){
        $file = request()->file('file');
        $src=imgUpload($file,false);
        $result=[
            "code"=>0,
            "mag"=>"上传成功",
            "data"=>[
                "src"=>$src,
            ]
        ];
        return json($result);
    }

}
